package jxc.base.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import jxc.base.helper.RootPath;

import eu.config.Config;
import eu.config.ConfigManager;

/**
 * 提供数据库连接
 * @author 吴振振
 *
 */
public class ConnectionProvider {
	
	/**
	 * 获取一个数据库连接
	 */
	public static Connection getConnection(){
		return newConnection();
	}
	
	/**
	 * 返回将数据库连接
	 * @param connection
	 */
	public static void returnConnection(Connection connection){
		try {
			connection.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	private static Connection newConnection(){
		try {
			return DriverManager.getConnection(url,user,pwd);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	//初始化
	static{
		try {
			Config config=ConfigManager.loadConfigFile(RootPath.root+"db.conf").getConfig("db");
			Class.forName(config.getValue("driverName"));
			url=config.getValue("dbURL");
			user=config.getValue("user");
			pwd=config.getValue("pwd");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private static String url;
	private static String user;
	private static String pwd;
	
}
